﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using DevExpress.XtraEditors.DXErrorProvider;
using Loki.UI;
using Loki.UI.Win;
using Luna.UI.Accounting;

namespace Luna.UI.Win.Accounting
{
    /// <summary>
    /// Account detail view.
    /// </summary>
    public partial class AccountDetailView : LunaView, IView<AccountDetailViewModel>
    {
        public AccountDetailView()
        {
            InitializeComponent();
            BindingChanged += AccountDetailView_BindingChanged;
        }

        protected override void BindFormName()
        {
            DataBinder.OneWay(this, f => f.Text, ViewModel, new Expression<Func<AccountDetailViewModel, object>>[] { v => v.IsDirty, v => v.Name }, new FormattedFormNameConverter(), Messages.UIValues.HEAD_ACCOUNT);
        }

        protected override IEnumerable<DXValidationProvider> Validators
        {
            get
            {
                return new DXValidationProvider[] { VAL_Save };
            }
        }

        private void AccountDetailView_BindingChanged(object P_Sender, EventArgs e)
        {
            TXT_Name.BindValue(ViewModel, v => v.Name);
            TXT_Number.BindValue(ViewModel, v => v.Number);
            TXT_Bank.BindValue(ViewModel, v => v.Bank);
            TXT_InitialAmount.BindValue(ViewModel, v => v.InitialAmount);
            TXT_StartDate.BindValue(ViewModel, v => v.StartDate);

            COL_Amount.Bind<DisplayableOperation>(d => d.Amount);
            COL_Category.Bind<DisplayableOperation>(d => d.CategoryID, EditorHelper.AccountCategory(ViewModel.SourceCategories), new ColumnRule<DisplayableOperation>() { Validator = x => x.CategoryID.HasValue, ErrorMessageBuilder = x => Messages.UIMessages.REQUIRED_ACCOUNT_CATEGORY });
            COL_Project.Bind<DisplayableOperation>(d => d.ProjectID, EditorHelper.Projects(ViewModel.SourceProjects, true));
            COL_Date.Bind<DisplayableOperation>(d => d.Date);
            COL_Description.Bind<DisplayableOperation>(d => d.Description);
            COL_ValueDate.Bind<DisplayableOperation>(d => d.ValueDate);

            VIEW_Operations.Configure(ViewModel.Operations, x => string.Format(Messages.UIMessages.DELETE_OPERATION, x.Description));
        }

        public new AccountDetailViewModel ViewModel
        {
            get { return (AccountDetailViewModel)base.ViewModel; }
            set { base.ViewModel = value; }
        }
    }
}
